package com.itheima.redbaby.engine.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.HashCodeBuilder;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.bean.Address;
import com.itheima.redbaby.bean.Area;
import com.itheima.redbaby.dao.AddressDao;
import com.itheima.redbaby.dao.domain.AddressDo;
import com.itheima.redbaby.engine.AddressEngine;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.factory.DaoAndEngineFactory;

public class AddressEngineImpl extends BaseEngine implements AddressEngine {

	private String userid;
	private Context context;
	Map<String, Object> map;
	private SharedPreferences sp;
	private AddressDao impl;

	public AddressEngineImpl() {
		if (GloableParams.isLogin) {
			userid = GloableParams.userid + "";
		} else {
			userid = "1";
		}
		impl = DaoAndEngineFactory.getImpl(AddressDao.class);
		this.context = GloableParams.context;
		sp = context.getSharedPreferences(ContantValue.VERSION_FILE,
				Context.MODE_PRIVATE);
	}

	public AddressEngineImpl(Context context) {
		this.context = context;
		impl = DaoAndEngineFactory.getImpl(AddressDao.class);
		sp = context.getSharedPreferences(ContantValue.VERSION_FILE,
				Context.MODE_PRIVATE);
	}

	@Override
	public List<AddressDo> getAddressList() {
		// 获取本地version
		float localVersion = sp.getFloat(ContantValue.VERSION_ADDRESS, 0f);
		map = new HashMap<String, Object>();
		map.put("Version", localVersion);

		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_ADDRESSLIST_URI, map, userid);
		List<AddressDo> addressList;
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("error")) {
				// 提示用户
			} else {
				Float serverVersion = json.getFloat("version");
				if (serverVersion > localVersion) {
					// 服务器版本大于客户端版本
					// 获取数据；持久化到本地，更改本地版本号；把List返回
					String strAddJson = json.getString("addresslist");
					addressList = JSON.parseArray(strAddJson, AddressDo.class);
					/*
					 * for (AddressDo address : addressList) {
					 * impl.insert(address); }
					 */

					Editor edit = sp.edit();
					edit.putFloat(ContantValue.VERSION_ADDRESS, serverVersion);

				} else {
					// 服务器版本号等于客户端版本号
					// 从本地获取数据，返回
					addressList = impl.findAll();

				}
				return addressList;
			}

		}
		return null;
	}

	@Override
	public List<Area> getAreaByParentId(int id) {
		map = new HashMap<String, Object>();
		map.put("id", id);
		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_ADDRESSAREA_URI, map);
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("error")) {
				// 提示用户
			} else {
				String strAreaJson = json.getString("arealist");
				List<Area> areaList = JSON.parseArray(strAreaJson, Area.class);
				return areaList;
			}

		}
		return null;

	}

	@Override
	public boolean setDefultAddress(int id) {
		map = new HashMap<String, Object>();
		map.put("id", id + "");
		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_ADDRESSDEFAULT_URI, map);
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("addressdefault")) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean deleteAddress(int id) {
		map = new HashMap<String, Object>();
		map.put("id", id + "");
		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_ADDRESSDELETE_URI, map);
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("addressdelete")) {
				return true;
			}
		}
		return false;
	}

	@Override
	public List<AddressDo> insertAddress(Map<String, Object> map) {
		JSONObject json = getJson(BaseEngine.TYPE_POST,
				ContantValue.REDBOY_ADDRESSSAVE_URI, map, userid);
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("addresssave")) {
				String strAddJson = json.getString("addresslist");
				return JSON.parseArray(strAddJson, AddressDo.class);
			}
		}
		return null;
	}

	@Override
	public String getAreaValue(int areaId) {
		map = new HashMap<String, Object>();
		map.put("areaid", areaId);
		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_AREA_URI, map);
		if (json != null) {
			return json.getString("result");
		}
		return null;
	}

	@Override
	public Map<String, Object> getMapAddress() {
		List<AddressDo> addressList = getAddressList();
		if (addressList != null) {
			map = new HashMap<String, Object>();
			Map<Integer, Object> mapValue = new HashMap<Integer, Object>();
			for (int i = 0; i < addressList.size(); i++) {
				AddressDo addressDo = addressList.get(i);
			/*	Map<String, Object> addMap = new HashMap<String, Object>();
				addMap.put("name", addressDo.getName());
				addMap.put("phone", addressDo.getPhonenumber());
				addMap.put("area", getAreaValue(addressDo.getAreaId()));
				addMap.put("areadetail", addressDo.getAreaDetail());*/
				mapValue.put(i, getAreaValue(addressDo.getAreaId()));
			}
			map.put("addressList", addressList);
			map.put("mapValue", mapValue);
			return map;
		}
		return null;
	}

	@Override
	public AddressDo getDefultAddress() {
		/*List<AddressDo> addressList = getAddressList();*/
		
		return null;
	}

}
